<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 15:22
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Resume;

class ResumeLogic extends BaseLogic
{

    private $ResumeModel;

    public function __construct()
    {
        parent::__construct();
        $this->ResumeModel = new Resume();
    }

    public function ResumeList($tempWhere, $nowPage){
        if ($tempWhere['title']){
            $where[] = ['title', 'like', "%".$tempWhere['title']."%"];
        }
        if (is_numeric($tempWhere['cate_id'])){
            $where[] = ['cate_id', '=', $tempWhere['cate_id']];
        }
        $count = $this->ResumeModel->where($where)->count();
        if ($count > 0){
            $ResumeList = $this->ResumeModel->where($where)->with(['ResumeCate'])->order('id desc')->page($nowPage, self::$pageNum)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $ResumeList, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    /**
     * 查找文章信息
     * @param $ResumeId
     * @return mixed
     */
    public function info($ResumeId){
        $info = $this->ResumeModel->get($ResumeId);
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该文章不存在');
        }
    }

    /**
     * 文章逻辑操作
     * @param $data
     * @return mixed
     */
    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        switch ($act){
            case 'add':
                $data['content'] = htmlspecialchars($data['content']);
                $res = $this->ResumeModel->allowField(true)->save($data);
                break;
            case 'edit':
                $data['content'] = htmlspecialchars($data['content']);
                $res = $this->ResumeModel->allowField(true)->save($data, $data['id']);
                break;
            case 'del':
                $Resume = $this->ResumeModel->get($data['id']);
                $res = $Resume->delete();
                break;
            case 'batch':
                $res = $this->ResumeModel->where('id','in',$data['ids'])->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}